package com.flyduck.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyduck.common.util.IdUtil;
import com.flyduck.common.util.StringUtil;
import com.flyduck.common.util.TokenUtil;
import com.flyduck.common.vo.PageResult;
import com.flyduck.system.dao.SysDictDataDao;
import com.flyduck.system.dao.SysDictTypeDao;
import com.flyduck.system.entity.SysDictDataEntity;
import com.flyduck.system.vo.dictData.DictOptionVO;
import com.flyduck.system.vo.dictData.SysDictDataReqVO;
import com.flyduck.system.vo.dictData.SysDictDataRespVO;
import com.flyduck.system.vo.dictData.trans.SysDictDataEntityToDictOptionVO;
import com.flyduck.system.vo.dictData.trans.SysDictDataEntityToSysDictDataRespVO;
import com.flyduck.system.vo.dictData.trans.SysDictDataReqVOToSysDictDataEntity;
import com.flyduck.system.vo.dictType.*;
import com.flyduck.system.vo.dictType.trans.SysDictTypeAddReqVOToSysDictTypeEntity;
import com.flyduck.system.vo.dictType.trans.SysDictTypeEntityToSysDictTypeDetailRespVO;
import com.flyduck.system.vo.dictType.trans.SysDictTypeEntityToSysDictTypeRespVO;
import com.flyduck.system.vo.dictType.trans.SysDictTypeUpdateReqVOToSysDictTypeEntity;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.flyduck.system.entity.SysDictTypeEntity;

import java.util.Date;
import java.util.List;

@Service
public class SysDictTypeService{


    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private SysDictTypeDao sysDictTypeDao;
    @Autowired
    private SysDictDataDao sysDictDataDao;


    /**
     * 获取字典类型分页
     * @param reqVo
     * @param page
     * @param size
     * @return
     */
    public PageResult<SysDictTypeRespVO> getDictTypePageList(SysDictTypeReqVO reqVo, Integer page, Integer size) {
        //************************* 1.构建分页条件 *************************
        Page<SysDictTypeEntity> dictTypeEntityPage = new Page<>(page, size);
        //************************* 2.构建查询条件 *************************
        QueryWrapper<SysDictTypeEntity> wrapper = getDictTypeQueryWrapper(reqVo);
        //************************* 3.查询 *************************
        IPage<SysDictTypeEntity> dictTypeEntityIPage = sysDictTypeDao.page(dictTypeEntityPage, wrapper);
        //************************* 4.结果返回转换 *************************
        List<SysDictTypeRespVO> respVOList = Lists.transform(dictTypeEntityIPage.getRecords(), new SysDictTypeEntityToSysDictTypeRespVO());
        return PageResult.ofPages(respVOList, dictTypeEntityIPage);
    }

    private QueryWrapper<SysDictTypeEntity> getDictTypeQueryWrapper(SysDictTypeReqVO reqVo) {
        QueryWrapper<SysDictTypeEntity> wrapper = new QueryWrapper<>();
        if (StringUtil.isNotBlank(reqVo.getDictName())) {
            wrapper.like("dict_name", reqVo.getDictName());
        }
        return wrapper;
    }

    /**
     * 获取字典类型详情
     * @param id
     * @return
     */
    public SysDictTypeDetailRespVO getDictTypeDetail(Long id) {
        SysDictTypeEntity dictTypeEntity = sysDictTypeDao.getById(id);
        SysDictTypeDetailRespVO sysDictTypeDetailRespVO = new SysDictTypeEntityToSysDictTypeDetailRespVO().apply(dictTypeEntity);
        List<SysDictDataEntity> dictDataEntityList = sysDictDataDao.findAllByDictKey(dictTypeEntity.getDictKey());
        List<SysDictDataRespVO> dataRespVOS = Lists.transform(dictDataEntityList, new SysDictDataEntityToSysDictDataRespVO());
        sysDictTypeDetailRespVO.setDictDataList(dataRespVOS);
        return sysDictTypeDetailRespVO;
    }

    /**
     * 新增字典类型
     * @param reqVo
     * @return
     */
    public boolean addDictType(SysDictTypeAddReqVO reqVo) {
        SysDictTypeEntity entity = new SysDictTypeAddReqVOToSysDictTypeEntity().apply(reqVo);
        entity.setId(IdUtil.getId());
        entity.setCreateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        entity.setCreateTime(new Date());
        entity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        entity.setUpdateTime(new Date());
        List<SysDictDataReqVO> dictDataList = reqVo.getDictDataList();
        List<SysDictDataEntity> dictDataEntityList = Lists.transform(dictDataList, new SysDictDataReqVOToSysDictDataEntity());
        sysDictDataDao.saveBatch(dictDataEntityList);
        return sysDictTypeDao.save(entity);
    }

    /**
     * 修改字典类型
     * @param reqVo
     * @return
     */
    public boolean updateDictType(SysDictTypeUpdateReqVO reqVo) {
        SysDictTypeEntity entity = new SysDictTypeUpdateReqVOToSysDictTypeEntity().apply(reqVo);
        entity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        entity.setUpdateTime(new Date());
        int rows = sysDictDataDao.deleteByDictKey(reqVo.getDictKey());
        List<SysDictDataReqVO> dictDataList = reqVo.getDictDataList();
        List<SysDictDataEntity> dictDataEntityList = Lists.transform(dictDataList, new SysDictDataReqVOToSysDictDataEntity());
        sysDictDataDao.saveBatch(dictDataEntityList);
        return sysDictTypeDao.updateById(entity);
    }

    /**
     * 删除字典类型
     * @param id
     * @return
     */
    public boolean deleteDictType(Long id) {
        SysDictTypeEntity entity = sysDictTypeDao.getById(id);
        sysDictDataDao.deleteByDictKey(entity.getDictKey());
        return sysDictTypeDao.removeById(id);
    }

    /**
     * 获取字典选项
     * @param dictKey
     * @return
     */
    public List<DictOptionVO> getDictOptionList(String dictKey) {
        List<SysDictDataEntity> dictDataEntityList = sysDictDataDao.findAllByDictKey(dictKey);
        return Lists.transform(dictDataEntityList,new SysDictDataEntityToDictOptionVO());
    }
}
